package site.marqstree.latte.ec.main.sort.list;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.util.ArrayList;

import site.marqstree.latte.ui.recycler.DataConverter;
import site.marqstree.latte.ui.recycler.ItemType;
import site.marqstree.latte.ui.recycler.MultipleFields;
import site.marqstree.latte.ui.recycler.MultipleItemEntity;

/*
 * 项目名: EleCommerce
 * 包名: site.marqstree.latte.ec.main.sort.list
 * 文件名: VerticalListDataConverter
 * 创建者: marqstree
 * 创建时间: 2020/1/29 13:40
 * 描述: 解析json到实体类
 * json格式：
 * {
    "code": 0,
    "message": "success",
    "data": {
      "list": [
        {
          "id": 1,
          "name": "分类1"
        },
        {
          "id": 2,
          "name": "分类2"
        },
        {
          "id": 3,
          "name": "分类3"
        }
    ]
 */
public final class VerticalListDataConverter extends DataConverter {

    private int mCurrentContentId = -1;

    public int getGetCurrentContentId(){
        return mCurrentContentId;
    }

    @Override
    public ArrayList<MultipleItemEntity> convert() {

        final ArrayList<MultipleItemEntity> dataList = new ArrayList<>();
        final JSONArray dataArray = JSON
                .parseObject(getJsonData())
                .getJSONObject("data")
                .getJSONArray("list");

        final int size = dataArray.size();
        for (int i = 0; i < size; i++) {
            final JSONObject data = dataArray.getJSONObject(i);
            final int id = data.getInteger("id");
            final String name = data.getString("name");

            final MultipleItemEntity entity = MultipleItemEntity.builder()
                    .setField(MultipleFields.ITEM_TYPE, ItemType.VERTICAL_MENU_LIST)
                    .setField(MultipleFields.ID, id)                    //id
                    .setField(MultipleFields.TEXT, name)                //name
                    .setField(MultipleFields.TAG, false)                //是否选中
                    .build();

            dataList.add(entity);
        }

        //设置第一个被选中
        dataList.get(0).setField(MultipleFields.TAG, true);
        //设置第一个选中的ContentID
        mCurrentContentId = dataList.get(0).getField(MultipleFields.ID);

        return dataList;
    }
}